package com.kunya.common.common;

import com.kunya.common.util.PropertiesUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisPool {
	private static JedisPool pool;
	private static Integer maxTotal=Integer.parseInt(PropertiesUtil.getProperty("redis.max.total"));
	private static Integer maxIdle=Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle"));
	private static Integer minIdle=Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle"));
	private static boolean testOnBorrow=Boolean.parseBoolean(PropertiesUtil.getProperty("redis.max.total"));
	private static boolean testOnReturn=Boolean.parseBoolean(PropertiesUtil.getProperty("redis.max.total"));
    
	private static String redisIp=PropertiesUtil.getProperty("redis1.ip");
	private static int redisPort=Integer.parseInt(PropertiesUtil.getProperty("redis1.port"));
	
	private static void initPool() {
		//设置连接池配置参数
		JedisPoolConfig config=new JedisPoolConfig();
		config.setMaxTotal(maxTotal);
		config.setMaxIdle(maxIdle);
		config.setMinIdle(minIdle);
		config.setTestOnBorrow(testOnBorrow);
		//初始化连接池
		pool=new JedisPool(config,redisIp,redisPort,1000*2);
	}
	
	
	static {
		//静态初始化 redis
		initPool();
		
	}
	
	public static Jedis getJedis() {
		//从连接池获取实例
		return pool.getResource();
	}
	
	
	public static void returnBrokenResource(Jedis jedis) {
			//返还连接池坏实例
			pool.returnBrokenResource(jedis);
		
	}
	public static void returnResource(Jedis jedis) {
			//返还连接池实例
			pool.returnResource(jedis);
		
	}
	
	
	public static void main(String[] args) {
		Jedis jedis=pool.getResource();
		jedis.set("zp","zp");
		returnResource(jedis);
		pool.destroy();
		System.out.println("end");
	}
	
}
